package com.shanling.shanlingcontroller.utils;

import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import butterknife.internal.Utils;

public class BluetoothUtil {

	/**
	 * 蓝牙是否开启
	 * @return
	 */
	public static boolean isBluetoothEnable() {
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if(mBluetoothAdapter == null){
			return false;
		}
		return mBluetoothAdapter.isEnabled();
	}
	
	/**
	 * 开启蓝牙
	 * @return 如果设备不支持蓝牙，返回false 
	 */
	public static boolean enableBluetooth(Context context) {
		BluetoothAdapter mBluetoothAdapter;
		if(Build.VERSION.SDK_INT <= 17) {
			mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		}
		else {
			BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
			mBluetoothAdapter = bluetoothManager.getAdapter();
		}
		if(mBluetoothAdapter == null){
			return false;
		}
		if(!mBluetoothAdapter.isEnabled()){
			mBluetoothAdapter.enable();
		}
		return true;
	}
	/**
	 * 关闭蓝牙
	 */
	public static void disableBluetooth(){
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if(mBluetoothAdapter == null){
			return;
		}
		if(mBluetoothAdapter.isEnabled()){
			mBluetoothAdapter.disable();
		}
	}

	public static String getMacAddressHeader(String macAddress) {
		if(TextUtils.isEmpty(macAddress)) {
			return null;
		}
		String[] data = macAddress.toUpperCase().split(":");
		if(data.length < 6) {
			return null;
		}
		return data[0]+data[1]+data[2];
	}



}
